package com.ziroom.finance.rabbitmq.common.util;

import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.validation.constraints.NotNull;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

/**
 * 读取配置文件类
 * Created by duxiaokang on 16/7/28.
 */
public class MyFileUtil {
    private static final Logger logger = LoggerFactory.getLogger(MyFileUtil.class);

    @SuppressWarnings("unchecked")
    public static <T> List<T> read(String path, @NotNull Class clazz) {
        List<T> result = Lists.newArrayList();

        InputStream in = FileUtil.class.getClassLoader().getResourceAsStream(path);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String line;
        try {
            while ((line = br.readLine()) != null) {
                if (clazz == String.class) {
                    result.add((T) line);
                }
            }
            in.close();
        } catch (Exception e) {
            logger.error("文件IO异常", e);
        }

        return result;
    }

    public static List<String> read(String path) {
        return read(path, String.class);
    }
}
